package com.yuand.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuand.config.Result;
import com.yuand.entity.User;
import com.yuand.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.WrappedPlainView;

/**
 * @author yuandong
 * @date 2021/8/27 16:15
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;


    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result<?> register(@RequestBody User user){
        //注册之前先查一下，数据库中有没有该用户
        User u = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if(u!=null){
            return Result.error("-1","用户名重复");
        }
        userMapper.insert(user);
        return Result.success();
    }


    /**
     * 登录页面
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user){
        User u = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if(u==null){
            return Result.error("-1","用户名或密码错误");
        }

        //登录成功后，前台需要将后台查到的用户信息缓存起来，这里将user对象返给前台
        //sessionStorage.setItem("user",JSON.stringify(res.data));  //登录成功后，将用户缓存起来
        return Result.success(u);
    }


    /**
     * 新增用户
     * @RequestBody  前台传递的json 转换成user对象
     * @return
     */
    @PostMapping("/save")
    public Result<?> save(@RequestBody User user){
        if(user.getPassword()==null||user.getPassword().equals("")){
            user.setPassword("123");
        }
        userMapper.insert(user);
        return Result.success();
    }


    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody User user){
        userMapper.updateById(user);
        return Result.success();
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public Result<?> del(@PathVariable int id){
        userMapper.deleteById(id);
        return Result.success();
    }


    /**
     * 分页查询
     * 如果前端没有传递参数，可以在这里使用defaultValue设置默认值
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping("/selectList")
    public Result<?> selectList(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam(defaultValue = "") String search){
        Page<User> page = new Page<>(pageNum, pageSize);
        Page<User> userPage = userMapper.selectPage(page, Wrappers.<User>lambdaQuery().like(User::getNickName, search));
        return Result.success(userPage);
    }


}
